﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.WSTMSystem.UControls
{

        public partial class UCircleButton : Button
        {
                public UCircleButton()
                {
                        InitializeComponent();
                        this.SetStyle(
                             ControlStyles.UserPaint |  //控件自行绘制，而不使用操作系统的绘制  
                             ControlStyles.AllPaintingInWmPaint | //忽略擦出的消息，减少闪烁。  
                             ControlStyles.OptimizedDoubleBuffer |//在缓冲区上绘制，不直接绘制到屏幕上，减少闪烁。  
                             ControlStyles.ResizeRedraw | //控件大小发生变化时，重绘。                    
                             ControlStyles.SupportsTransparentBackColor, true);//支持透明背景颜色  
                        this.Font = new Font("微软雅黑", 10, FontStyle.Bold);
                        this.FlatStyle = FlatStyle.Flat;
                        this.Size = new Size(110, 50);

                }

                private const int WM_PAINT = 0xF;


                private string btnText;
                [DefaultValue(typeof(string), "按钮文本"), Description("控件文本")]
                public string BtnText
                {
                        get { return btnText; }
                        set
                        {
                                btnText = value;
                                this.Text = btnText;
                        }
                }

                private Color borderColor = Color.Red;
                [DefaultValue(typeof(Color), "Red"), Description("控件边框颜色")]
                public Color BorderColor
                {
                        get { return borderColor; }
                        set
                        {
                                borderColor = value;
                                base.Invalidate();
                        }
                }

                [DefaultValue(typeof(Color), "MediumSlateBlue"), Description("控件背景颜色")]
                private Color bgColor = Color.MediumSlateBlue;
                public Color BgColor
                {
                        get { return bgColor; }
                        set
                        {
                                bgColor = value;
                                base.Invalidate();
                        }
                }

                [DefaultValue(typeof(Color), "Gray"), Description("控件背景颜色2")]
                private Color bgColor2 = Color.White;
                public Color BgColor2
                {
                        get { return bgColor2; }
                        set
                        {
                                bgColor2 = value;
                                base.Invalidate();
                        }
                }


                [DefaultValue(typeof(int), "5"), Description("圆角弧度大小")]
                private int radius = 5;
                public int Radius
                {
                        get { return radius; }
                        set
                        {
                                radius = value;
                                base.Invalidate();
                        }
                }

                [DefaultValue(typeof(LinearGradientMode), "Vertical"), Description("渐变方式")]
                private LinearGradientMode gradientMode = LinearGradientMode.Vertical;
                public LinearGradientMode GradientMode
                {
                        get { return gradientMode; }
                        set
                        {
                                gradientMode = value;
                                base.Invalidate();
                        }
                }

                private Color fontColor = Color.Black;
                [DefaultValue(typeof(Color), "Black"), Description("控件文本颜色")]
                public Color FontColor
                {
                        get { return fontColor; }
                        set
                        {
                                fontColor = value;
                                base.Invalidate();
                        }
                }
                protected override void WndProc(ref Message m)
                {
                        try
                        {
                                base.WndProc(ref m);
                                if (m.Msg == WM_PAINT)
                                {
                                        using (Graphics g = Graphics.FromHwnd(this.Handle))
                                        {
                                                g.SmoothingMode = SmoothingMode.AntiAlias;
                                                Rectangle r = new Rectangle();
                                                r.Width = this.Width;
                                                r.Height = this.Height;
                                                DrawBorder(g, r, this.Radius);

                                        }
                                }
                        }
                        catch (Exception ex)
                        {
                                MessageBox.Show(ex.Message);
                        }
                }

                private void DrawBorder(Graphics g, Rectangle rect, int radius)
                {
                        GraphicsPath path = new GraphicsPath();
                        path = PaintCommon.GetRoundRectangle(rect, radius);
                        if (this.FlatAppearance.BorderSize > 0)
                        {
                                using (Pen pen = new Pen(this.BorderColor, 1))
                                {
                                        g.DrawPath(pen, path);
                                }
                        }

                        Rectangle rect1 = new Rectangle(rect.X + 1, rect.Y + 1, rect.Width - 2, rect.Height - 2);

                        DrawBackColor(g, rect1, radius);
                        StringFormat strFormat = new StringFormat();//文本格式
                        strFormat.LineAlignment = StringAlignment.Center;//垂直居中
                        strFormat.Alignment = StringAlignment.Center;//水平居中
                        Rectangle recHead = new Rectangle(5, 5, this.Width - 10, this.Height - 10);//矩形
                        g.DrawString(this.BtnText, this.Font, new SolidBrush(this.FontColor), recHead, strFormat);

                }

                private void DrawBackColor(Graphics g, Rectangle rect, int radius)
                {
                        GraphicsPath path1 = PaintCommon.GetRoundRectangle(rect, radius);
                        if (this.BgColor2 != Color.Transparent)
                        {
                                //线型渐变
                                LinearGradientBrush brush = new LinearGradientBrush(rect, BgColor, BgColor2, gradientMode);
                                g.FillPath(brush, path1);
                        }
                        else
                        {

                                Brush b = new SolidBrush(BgColor);
                                g.FillPath(b, path1);
                        }
                }

                protected override void OnMouseEnter(EventArgs e)//鼠标进入时
                {
                        base.OnMouseEnter(e);

                }
                protected override void OnMouseLeave(EventArgs e)//鼠标离开
                {
                        base.OnMouseLeave(e);

                }


                protected override bool ShowFocusCues
                {
                        get
                        {
                                return false;
                        }
                }

                protected override bool ShowKeyboardCues
                {
                        get
                        {
                                return false;
                        }
                }

                protected override void OnMouseClick(MouseEventArgs e)
                {
                        base.OnMouseClick(e);
                        //BorderColor = Color.Transparent;
                }





        }
}
